package com.tj.jinzao.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.tj.jinzao.R;
import com.tj.jinzao.activity.PersonArticleActivity;
import com.tj.jinzao.base.BaseListAdapter;
import com.tj.jinzao.common.Constant;
import com.tj.jinzao.model.Article;
import com.tj.jinzao.widgets.RoundedImageView;

import java.text.SimpleDateFormat;
import java.util.concurrent.CopyOnWriteArrayList;


/**
 * 首页日记文章 Adapter
 * Created by wjy on 2018/10/12.
 */
public class MyMessageTypeListItemAdapter extends BaseListAdapter<Article, MyMessageTypeListItemAdapter.ViewHolder> {

    private SimpleDateFormat mSimpleDateFormat;
    private String mType;

    public MyMessageTypeListItemAdapter(Context context, CopyOnWriteArrayList data,String mType) {
        super(context, data);
        mSimpleDateFormat = new SimpleDateFormat("MM年dd日");
        this.mType=mType;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_my_message_type, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Article message = mData.get(position);
        if(message!=null){
            holder.content.setVisibility(View.VISIBLE);
            if(mType.equals("2")){
                if (message.getType()==1) {
                    if(message.isHasCollection()){
                        if(message.getUser()!=null&&message.getUser().getNickname()!=null){
                            holder.hint.setText(message.getUser().getNickname()+"收藏了你的日记");
                        }else {
                            holder.hint.setText("xx收藏了你的日记");
                        }

                    }else {
                        if(message.getUser()!=null&&message.getUser().getNickname()!=null){
                            holder.hint.setText(message.getUser().getNickname()+"赞了你的日记");
                        }else {
                            holder.hint.setText("xx赞了你的日记");
                        }
                    }

                } else {
                    if(message.isHasCollection()){
                        if(message.getUser()!=null&&message.getUser().getNickname()!=null){
                            holder.hint.setText(message.getUser().getNickname()+"收藏了你的话题");
                        }else {
                            holder.hint.setText("xx收藏了你的话题");
                        }
                    }else {
                        if(message.getUser()!=null&&message.getUser().getNickname()!=null){
                            holder.hint.setText(message.getUser().getNickname()+"赞了你的话题");
                        }else {
                            holder.hint.setText("xx赞了你的话题");
                        }
                    }
                }
                holder.name.setText("");
                holder.content.setVisibility(View.GONE);
            }else {
                holder.content.setVisibility(View.VISIBLE);
                holder.hint.setText("评论了你");
                if(message.getComment_content()!=null){
                    holder.content.setText(message.getComment_content());
                }else {
                    holder.content.setText("");
                }
                if(message.getUser()!=null&&message.getUser().getNickname()!=null){
                    holder.name.setText(message.getUser().getNickname());
                }else {
                    holder.name.setText("");
                }

            }
            if(message.getUser()!=null&&message.getUser().getImgurl()!=null){
                Glide.with(mContext).load(Constant.IMAGE_PREFIX+message.getUser().getImgurl()).into(holder.head);
            }else {
                holder.head.setImageResource(R.mipmap.default_head_women);
            }

            Glide.with(mContext).load(Constant.IMAGE_PREFIX+message.getImgurls()).into(holder.url);
            holder.time.setText(message.getReleaseTime());
            holder.head.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mContext.startActivity(new Intent(mContext, PersonArticleActivity.class)
                            .putExtra("user", mData.get(position).getUser()));

                }
            });
            holder.content_lay.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    if(onLongClickLinstener!=null){
                        onLongClickLinstener.onLongClick(mData.get(position).getId());
                    }
                    return false;
                }
            });

        }
    }
    public OnLongClickLinstener onLongClickLinstener;
    public void setOnLongClickLinstener(OnLongClickLinstener onLongClickLinstener){
        this.onLongClickLinstener=onLongClickLinstener;
    }
    public interface OnLongClickLinstener{
        void onLongClick(Long id);
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView name, content, time, hint;
        RoundedImageView head;
        ImageView url;
        LinearLayout content_lay;

        public ViewHolder(View itemView) {
            super(itemView);
            name = itemView.findViewById(R.id.tv_name);
            content = itemView.findViewById(R.id.tv_content);
            hint = itemView.findViewById(R.id.tv_hint);
            time = itemView.findViewById(R.id.tv_time);
            head = itemView.findViewById(R.id.iv_head);
            url = itemView.findViewById(R.id.iv_url);
            content_lay = itemView.findViewById(R.id.content_lay);
        }
    }
}
